import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import userStore from '@/store/user'
import nprogress from "nprogress";
import 'nprogress/nprogress.css'


const routes: RouteRecordRaw[] = [
    {
        path: '/login',
        component: () => import('@/pages/Login/Login.vue')
    },
    {
        path: '/home',
        component: () => import('@/pages/Home/Home.vue'),
        meta: { auth: false }
    },
    {
        path: '/chat',
        component: () => import('@/pages/Chat/index.vue'),
        meta: { auth: false }
    }
]

const router = createRouter({
    routes,
    history: createWebHistory()
})

router.beforeEach((to, from, next) => {
    nprogress.start()
    const token = sessionStorage.getItem("token")
    if (to.path == '/login'||to.path == '/h5/login') {
        next()
    } else if (!token) {
        next('/h5/login')
    }
    else {
        next()
    }
})
router.afterEach(() => {
    nprogress.done()
})

export default router